<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth,App\User,App\permission;
class PermsController extends Controller
{
    public $authRole;
    public $auth;
    public function __construct()
    {
        $this->middleware('auth');

        if($this->auth = Auth::user()){
            $this->authRole = User::find($this->auth->id)->roles[0];
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $authRole = $this->authRole;
        $perms = Permission::orderBy('id')->paginate(5);

        return view('admin.perm',compact('authRole','perms'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $authRole = $this->authRole;
        return view('admin.addPerms',compact('authRole'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,
            ['name' =>  'required|min:2|max:20|unique:roles,name','display_name' =>  'required']
        );
        $perm = new \App\Permission();
        $perm->name         = $request->name;
        $perm->display_name = $request->display_name; // optional
        $perm->description  = $request->description; // optional
        $perm->save();
        Auth::user()->attachPermission($perm);
        return redirect()->back()->withInfo('创建角色成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $authRole = $this->authRole;
        $perm = Permission::find($id);
        return view('admin.editPerms',compact('authRole','perm'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,
            ['name' =>  'required|min:2|max:20|unique:permissions,name,'.$id,'display_name' =>  'required']
        );
        $info = $request->only('name','display_name','description');

        $perm = Permission::findOrFail($id);

        $perm->update($info);

        return redirect()->back()->withInfo('修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $perm = Permission::findOrFail($id);
        $perm->roles()->sync([]);
        $perm->delete();
        return redirect()->back()->withInfo('删除成功');

    }
}
